package net.digitalprimates.flex2.uint.utils
{
	import flash.display.Loader;
	import flash.errors.IllegalOperationError;
	import flash.events.Event;
	import flash.events.EventDispatcher;
	import flash.events.IOErrorEvent;
	import flash.events.SecurityErrorEvent;
	import flash.net.URLRequest;
	import flash.system.ApplicationDomain;
	import flash.system.LoaderContext;
	
    [Event(name="LoadComplete", type="flash.events.Event")]	
    [Event(name="LoadError", type="flash.events.Event")]
	
	public class ClassLoader extends EventDispatcher
	{
		
		//------------------------------------------
		//	Events	
		//------------------------------------------
		
        public static var LOAD_COMPLETE:String = "LoadComplete";
        public static var LOAD_ERROR:String = "LoadError";
        
        
		//------------------------------------------
		//	Variables	
		//------------------------------------------
        
        private var _loader:Loader;
        private var _swfLib:String;
        private var _request:URLRequest;
        private var _loadedClass:Class;
		
		
		
		//------------------------------------------
		//	Constructor
		//------------------------------------------
		
		public function ClassLoader()
		{
			init();
		}
		
		
		private function init() : void
		{
			
            _loader = new Loader();
            _loader.contentLoaderInfo.addEventListener( Event.COMPLETE, onLoadSWFComplete );
            _loader.contentLoaderInfo.addEventListener( IOErrorEvent.IO_ERROR, onLoadSWFError );
            _loader.contentLoaderInfo.addEventListener( SecurityErrorEvent.SECURITY_ERROR, onLoadSWFSecurityError );
		}	
		
		
		//------------------------------------------
		//	Public methods
		//------------------------------------------
		
		public function load( lib_:String ) : void
		{
			_swfLib = lib_;
			_request = new URLRequest( _swfLib );
			
			//	We create a security context here for the same domain sandbox
			var context:LoaderContext = new LoaderContext();
				context.applicationDomain = ApplicationDomain.currentDomain;
				
			_loader.load( _request, context );
		}


		public function getClass( className_:String ) : Class
		{
			try
			{
				return _loader.contentLoaderInfo.applicationDomain.getDefinition( className_ ) as Class;
			} catch( e:Error ) {
				throw new IllegalOperationError( className_ + " definition not found in " + _swfLib );
			}
			
			return null;
		}
		
		
		//------------------------------------------
		//	Loader Event Handlers	
		//------------------------------------------
		
		private function onLoadSWFComplete( e:Event ) : void
		{
			dispatchEvent( new Event( ClassLoader.LOAD_COMPLETE ) );
		}
		
		private function onLoadSWFError( e:IOErrorEvent ) : void
		{
			dispatchEvent( new Event( ClassLoader.LOAD_ERROR ) );
		}
		
		private function onLoadSWFSecurityError( e:SecurityErrorEvent ) : void
		{
			dispatchEvent( new Event( ClassLoader.LOAD_ERROR ) );
		}
	}

}